package hy.client;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class CoreNet {
	private Socket socket;
	public static final String HOST = "1.wangerweichat.sinaapp.com" ;
	public static final int  PORT = 80;
	public CoreNet(){
//		try {
//			socket = new Socket(HOST, PORT);
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	
	// 轮询特定的网址，抓取命令调用CmdProcesseror的process(String[])方法
	// http://1.wangerweichat.sinaapp.com/CmdProductor?key=hy
	public String getCMD() throws UnknownHostException, IOException {
		
		if (socket != null)
			socket.close();
		//由于现代服务器一般是http1.1，每个连接会维持一段时间，所以这里判断一下如果服务器关闭了连接，那么重连。
//		if(socket.isClosed()){
//			socket.close();
//			socket = new Socket(HOST, PORT);
//		}
		socket = new Socket(HOST, PORT);
		StringBuffer get = new StringBuffer();
		OutputStreamWriter osw = new OutputStreamWriter(
				socket.getOutputStream());
		//http请求头
		get.append("GET /CmdProductor?key=hy HTTP/1.1\r\n");
		get.append("Host: 1.wangerweichat.sinaapp.com\r\n");
		get.append("Connection: Keep-Alive\r\n");
		get.append("\r\n");
		osw.write(get.toString());
		osw.flush();
		
		//创建输入流读取资料
		BufferedInputStream is = new BufferedInputStream(
				socket.getInputStream());
//		String res;
		while( !(readLine(is)).equals("")){
//			System.out.println(res);
		}
		
		
		StringBuilder cmd = new StringBuilder();
		//得到命令的长度
//		int cmdLen = (is.read()) - 48;
		String tmp = readLine(is);
//		System.out.println("tmp + " + tmp);
		int cmdLen = Integer.valueOf(tmp, 16);
//		readLine(is);
		while(cmdLen-- > 0){
//			System.out.print((char)is.read());
			cmd.append((char)(is.read()));
		}
//		while(!readLine(is).equals("0")){			
//		}

		return cmd.toString();
	}
	
	private String readLine(InputStream is){
		int buf;
		StringBuilder sb = new StringBuilder();
		try {
			while((buf = is.read()) != -1){
				if(buf == '\r'){
					is.read();
					break;
				}
				sb.append((char)buf);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	public static void main(String[] args) throws UnknownHostException,
			IOException, InterruptedException {
			
			CoreNet.driver(1000);
	}
	
	public static void driver(int limit) throws UnknownHostException, IOException, InterruptedException{
		CoreNet core = new CoreNet();
		CmdProcessor processor = new CmdProcessor();
		for(int i =0; i < limit; i++){
	//		System.out.print(i+"---");
			String str = core.getCMD().trim();
			if(str.equals("nocmd"))
				continue;
			String[] cmd = str.split(" ");
			for(int j = 0; j < cmd.length; j++){
				System.out.print(cmd[j] + " ");
			}
			System.out.println();
			processor.process(cmd);
			Thread.sleep(1000*3);
		}
	}
}
